// import { App } from "./app";
// import { UILayer } from "./UILayer";
// import {BulkLoader} from './utils/BulkLoader';
// import {ImageLoader} from './utils/ImageLoader';
///<reference path='./app.ts'/>
///<reference path='./UILayer.ts'/>
///<reference path='./utils/BulkLoader.ts'/>
///<reference path='./utils/ImageLoader.ts'/>
interface PlanetPercent {
    percent: number;
    planetImage: HTMLImageElement;
}
class GameStar extends lg.Container {
    private _canvas: HTMLCanvasElement;
    private _earth: lg.Bitmap;
    private _topPlanet: lg.Bitmap;
    private _planetsData: Array<PlanetPercent>;
    public static moveY: number = 0;
    public static lastMoveSpeed: number = 0;
    private _tweens: Array<any> = new Array<any>();
    private _lastUFOY: number = 0;

    private _ufos: Array<lg.Bitmap> = new Array<lg.Bitmap>();
    private _intervalFPS = 500;
    // private _intervalCount = 0;
    private _hasStoped: boolean;//否则停顿 
    private _ufoStrings: Array<string> = new Array<string>();
    constructor() {
        super();

        this._planetsData = new Array<PlanetPercent>();
        // main-moon
        // main-mercury
        // main-venus
        // main-sun
        // main-mars
        // main-jupiter
        // main-saturn

        var psd = window["PlanetPercents"];
        for (var i = 0; i < psd.length; i++) {
            var pd = psd[i];
            this._planetsData.push(<PlanetPercent>{ percent: pd.percent, planetImage: App.loadManager.getImage(pd.starName) });
        }
        this.addEventListener("added", this.addedHandler);


    }
    private ufoHandler() {

        var ufosArr = window["ufosArr"];
        for (var i = 0; i < ufosArr.length; i++) {
            var ufo = ufosArr[i];
            this._ufoStrings.push(ufo.name);
        }
        // this._ufoStrings.push("meteorolite1");
        // this._ufoStrings.push("meteorolite2");
        // this._ufoStrings.push("meteorolite3");
        // this._ufoStrings.push("celestial-body1");
        // this._ufoStrings.push("celestial-body2");
        // this._ufoStrings.push("astronaut");
        // this._ufoStrings.push("satellite");
        // // this._ufoStrings.push("star");
        // this._ufoStrings.push("shooting-star");
        // shooting-star
        setInterval(() => {
            // this._intervalCount++;
            if (//this._intervalCount % this._intervalFPS == 0 
                Math.random() * this._intervalFPS < 2
                && GameStar.moveY != 0
            ) {
                if (this._ufos.length > 20) {
                    this.clearUFO();
                    //console.log("清理ufo");
                }

                var nextY: number = this.y * -1 - 100;
                if (Math.abs(nextY) - Math.abs(this._lastUFOY) > this._canvas.height / 3) {
                    var random: number = Math.floor(Math.random() * this._ufoStrings.length);

                    var name: string = this._ufoStrings[random];
                    //console.log(name);
                    var b: lg.Bitmap = new lg.Bitmap(App.loadManager.getImage(name));
                    this.addChild(b);
                    this._ufos.push(b);
                    b.x = Math.random() * this._canvas.width;
                    this._lastUFOY = b.y = this.y * -1 - 100;
                    var targetX: number = b.x > this._canvas.width * 0.5 ? -300 : this._canvas.width + 300;

                    // console.log(b.y)
                    this._tweens.push(TweenMax.to(b, Math.random() * 15, { x: targetX, ease: Linear.easeNone }));
                }
            }
        });
    }
    private clearUFO() {
        for (var i = 0; i < this._ufos.length; i++) {
            var o = this._ufos[i];
            // TweenLite.kill(o);
            o.parent.removeChild(o);
        }
        this._ufos = new Array<lg.Bitmap>();
    }
    private addedHandler(e): void {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.initView();
        e.target.initEvent();
    }
    private initEvent(): void {
        this.addEventListener("tick", () => {
            TweenLite.to(this, 1, { y: this.y + GameStar.moveY });
        });
    }
    private initView() {
        this._canvas = <HTMLCanvasElement>(this.stage.canvas);



        // this._earth.y= 0;
        // console.log(this.localToGlobal(0,this._earth.y));
        // console.log(this._earth.image.height,this.stage.getBounds().height)
        this._earth = new lg.Bitmap(App.loadManager.getImage("earth"));
        this.clearUFO();
        this.initData();
        this.addChild(this._earth);

        this.ufoHandler();
        setInterval(() => {
            //运动停顿

            let localP: lg.Point = this.localToGlobal(0, this._earth.y);

            var updateFuc: Function = () => {
                this._earth.y = this.globalToLocal(localP.x, localP.y).y;
            }
            var completeFunc: Function = () => {
                this._earth.rotation = 0;
                var px: number = Math.random() > 0.5 ? this._earth.image.width * -1 : this._canvas.width + this._earth.image.width;
                TweenMax.to(this._earth, 3, { x: px });
            };

            var firstCompleteFunc: Function = () => {
                this._earth.rotation = 40;
                TweenMax.to(this._earth, 0.1, { rotation: -40, yoyo: true, repeat: 6, onComplete: completeFunc });
                UILayer.eatSoundPlay();
            }
            if (localP.y > this._canvas.height * 0.5 && !this._hasStoped) {
                this._hasStoped = true;

                TweenMax.to(this._earth, 0.5, { onUpdate: updateFuc, scaleX: 0.5, scaleY: 0.5, yoyo: true, repeat: 1, onComplete: firstCompleteFunc });

            }
        })
    }
    private initData() {
        for (var i: number = 0; i < this._tweens.length; i++) {
            let v: TweenLite = this._tweens[i];
            v.kill(this);
            if (v)
                TweenLite.killTweensOf(v, false);
            this._tweens[i] = null;
            delete this._tweens[i];
        }
        this._tweens = new Array<any>();
        GameStar.moveY = 0;
        GameStar.lastMoveSpeed = 0
        
        this._earth.image = <HTMLImageElement>App.loadManager.getImage("earth");
        // alert(this._earth.image.width)
        this._earth.regX = this._earth.image.width >> 1;
        this._earth.regY = this._earth.image.height >> 1;
        // console.log(this._earth.regX,this._earth.regY)
        this._earth.x = this._canvas.width - this._earth.image.width * 0.5;
        this._earth.y = this._canvas.height - this._earth.image.height * 0.5 - this.y;
        this._earth.scaleX = this._earth.scaleY = 1;
        // console.log(this._earth.x,this._earth.y)
        this._earth.visible = false;
        this.y = 0;
        setTimeout(() => {
            this._earth.y = this._canvas.height - this._earth.image.height * 0.5 - this.y;
            this._earth.scaleX = this._earth.scaleY = 1;
            this.y = 0;
            this._earth.visible = true;
            // console.log(this._earth.y, this.localToGlobal(0, this._earth.y), this.y, this._canvas.height)
        }, 100);

        this._hasStoped = true;


        // this.y=0;
        //console.log(this._earth.regY,this._earth.image);
        // console.log(this._earth.image)
        this.clearUFOs();

    }
    private clearUFOs() {
        // this._intervalCount=0;
        for (var i = 1; i < this._ufos.length; i++) {
            this._ufos[i].parent.removeChild(this._ufos[i]);
        }
        this._ufos = new Array<lg.Bitmap>();

    }
    public setScore(v: number): void {
        if (v == 0) {
            this.initData();

            return;
        }
        // console.log(v)
        for (var i = 0; i < this._planetsData.length; i++) {
            var p: PlanetPercent = this._planetsData[i];
            if (p.percent == v) {

                this._earth.image = p.planetImage;
                this._hasStoped = false;
                var point: lg.Point = this.localToGlobal(0, 0);
                this._earth.regX = this._earth.image.width >> 1;
                this._earth.regY = this._earth.image.height >> 1;
                this._earth.scaleX = this._earth.scaleY = 1;
                this._earth.y = point.y * -1 - this._earth.image.height * 0.5;
                this._earth.x = this._canvas.width * 0.5;
                var pp: PlanetPercent = this._planetsData[i - 1];
                var prePrecent: number;
                if (pp) {
                    prePrecent = pp.percent;
                }
                else {
                    prePrecent = 0;
                }
                //GameStar.lastMoveSpeed=GameStar.moveY=((this._canvas.height+this._earth.image.height*2)/(p.percent-prePrecent))*0.5;
                //    / console.log(GameStar.lastMoveSpeed);
            }
        }
        // GameStar.moveY=GameStar.lastMoveSpeed;
        this._tweens.push(TweenLite.to(this, 1, { y: this.y + GameStar.moveY }));

    }
}